import store from "../store/store";
import { getDicts } from '../api/dict/data'
import { getDict, setDict, removeDict, cleanDict } from "../store/actions/dict";
import { GET_DICT, SET_DICT, REMOVE_DICT, CLEAN_DICT} from "../store/constant";

/**
 * 获取字典数据
 */
export function useDict(...args) {
  const res ={};
  return (() => {
    args.forEach((dictType, index) => {
      res[dictType] = [];
      const dicts = store.getState().dictReducer;
      //console.log('====useDict==1===',dicts)
      const dictSelected = dicts.find(p => p.key === dictType);
    if (dictSelected) {
        res[dictType] = dictSelected;
        } else {
        getDicts(dictType).then(resp => {
            //console.log('====useDict==resp===',resp)
            res[dictType] = resp.data.map(p => ({ label: p.dictLabel, value: p.dictValue, elTagType: p.listClass, elTagClass: p.cssClass }))
            store.dispatch({type: SET_DICT, key:dictType, value: res[dictType]});
        })
        }
    });
    return res;
  })()
}